{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "There is already a running event loop.\n",
      "\u001b[1m\u001b[35m Flow started with ID: 8152d03c-c2de-4261-88fe-e6b7da2e7066\u001b[00m\n",
      "Starting flow\n",
      "Flow State ID: 8152d03c-c2de-4261-88fe-e6b7da2e7066\n",
      "Random City: Lanzhou\n",
      "Generated fun fact: 兰州是中国甘肃省的省会，也以“牛肉面”（拉面）而闻名。一碗好的兰州拉面讲究“一清二白三红四绿五黄”，味道鲜美，是当地非常受欢迎的传统美食。\n",
      "Plot saved as my_flow_plot.html\n"
     ]
    }
   ],
   "source": [
    "\n",
    "from crewai.flow.flow import Flow, listen, start\n",
    "from dotenv import load_dotenv\n",
    "from litellm import completion\n",
    "    \n",
    "# response = completion(\n",
    "#     model = 'ollama/qwen2.5:14b',\n",
    "#     messages=[{ \"content\": \"用20个字介绍你是谁?\",\"role\": \"user\"}], \n",
    "#     api_base=\"http://192.168.20.43:11434\"\n",
    "# )\n",
    "# print(response)\n",
    "\n",
    "class ExampleFlow(Flow):\n",
    "    model = 'ollama/qwen2.5:14b'\n",
    "\n",
    "    @start()\n",
    "    def generate_city(self):\n",
    "        print(\"Starting flow\")\n",
    "        # Each flow state automatically gets a unique ID\n",
    "        print(f\"Flow State ID: {self.state['id']}\")\n",
    "\n",
    "        response = completion(\n",
    "            model=self.model,\n",
    "            api_base=\"http://192.168.20.43:11434\",\n",
    "            messages=[\n",
    "                {\n",
    "                    \"role\": \"user\",\n",
    "                    \"content\": \"Return the name of a random city in the China.\",\n",
    "                },\n",
    "            ],\n",
    "        )\n",
    "\n",
    "        random_city = response[\"choices\"][0][\"message\"][\"content\"]\n",
    "        # Store the city in our state\n",
    "        self.state[\"city\"] = random_city\n",
    "        print(f\"Random City: {random_city}\")\n",
    "\n",
    "        return random_city\n",
    "\n",
    "    @listen(generate_city)\n",
    "    def generate_fun_fact(self, random_city):\n",
    "        response = completion(\n",
    "            model=self.model,\n",
    "            api_base=\"http://192.168.20.43:11434\",\n",
    "            messages=[\n",
    "                {\n",
    "                    \"role\": \"user\",\n",
    "                    \"content\": f\"Tell me a fun fact about {random_city}, Reply in Chinese.\",\n",
    "                },\n",
    "            ],\n",
    "        )\n",
    "\n",
    "        fun_fact = response[\"choices\"][0][\"message\"][\"content\"]\n",
    "        # Store the fun fact in our state\n",
    "        self.state[\"fun_fact\"] = fun_fact\n",
    "        return fun_fact\n",
    "\n",
    "import asyncio\n",
    "if asyncio.get_running_loop():\n",
    "    print(\"There is already a running event loop.\")\n",
    "    loop = asyncio.get_running_loop()\n",
    "\n",
    "flow = ExampleFlow()\n",
    "\n",
    "import threading\n",
    "\n",
    "def run_in_thread():\n",
    "    result = flow.kickoff()\n",
    "    print(f\"Generated fun fact: {result}\")\n",
    "    flow.plot(\"my_flow_plot\")\n",
    "\n",
    "thread = threading.Thread(target=run_in_thread)\n",
    "thread.start()\n",
    "thread.join()\n",
    "\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
